<!--
 * @Author: zulezhe
 * @Date: 2021-01-29 15:29:45
 * @LastEditors: zulezhe
 * @LastEditTime: 2021-02-04 14:45:36
 * @Description: In User Settings Edit
 * @FilePath: \canvas\02-进阶\03.清除.html
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      html,
      body {
        width: 100%;
        height: 100%;
      }
      * {
        margin: 0;
        padding: 0;
      }
      #canvas-container {
        width: 100%;
        height: 100%;
      }
      canvas {
        outline: 1px dashed red;
      }
    </style>
  </head>
  <body>
    <div id="canvas-container"></div>
    <script src="./index.js"></script>
    <script>
      const mycanvas = new MyCanvas("canvas-container", document.body.offsetWidth, document.body.offsetHeight);
      const context = mycanvas.getContext();
      clearRect(context);
      /**
       * 可以清除canvas画布上某一块矩形区域变成透明的,可以制作橡皮檫或者在动画中使用
       * context.clearRect(x, y, width, height);
       * xNumber
            矩形左上角x坐标。
            yNumber
            矩形左上角y坐标。
            widthNumber
            被清除的矩形区域的高度。
            heightNumber
            被清除的矩形区域的宽度度
       *
       */
      function clearRect(context) {
        const img = new Image();
        img.src = "./images/img1.jpg";
        img.onload = function () {
          context.drawImage(img, 100, 100, img.width, img.height);
          context.clearRect(250, 250, 100, 100);
        };
      }
    </script>
  </body>
</html>
